home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / iovfscanf.c < prev    next >
C/C++ Source or Header  |  1994-10-13  |  19KB  |  785 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /*
  26.  * Copyright (c) 1990 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42. /* Extensively hacked for GNU iostream by Per Bothner 1991, 1992, 1993.
  43.    Changes copyright Free Software Foundation 1992, 1993. */
  44.  
  45. #if defined(LIBC_SCCS) && !defined(lint)
  46. static char sccsid[] = "%W% (Berkeley) %G%";
  47. #endif /* LIBC_SCCS and not lint */
  48.  
  49. #include <libioP.h>
  50. #include <ctype.h>
  51. #ifdef __STDC__
  52. #include <stdarg.h>
  53. #else
  54. #include <varargs.h>
  55. #endif
  56.  
  57. #ifndef    NO_FLOATING_POINT
  58. #define FLOATING_POINT
  59. #endif
  60.  
  61. #ifdef FLOATING_POINT
  62. #include "floatio.h"
  63. #define    BUF    (MAXEXP+MAXFRACT+3)    /* 3 = sign + decimal point + NUL */
  64. #else
  65. #define    BUF    40
  66. #endif
  67.  
  68. /*
  69.  * Flags used during conversion.
  70.  */
  71. #define    LONG        0x01    /* l: long or double */
  72. #define    LONGDBL        0x02    /* L: long double; unimplemented */
  73. #define    SHORT        0x04    /* h: short */
  74. #define    SUPPRESS    0x08    /* suppress assignment */
  75. #define    POINTER        0x10    /* weird %p pointer (`fake hex') */
  76. #define    NOSKIP        0x20    /* do not skip blanks */
  77. #define    WIDTH        0x40    /* width */
  78.  
  79. /*
  80.  * The following are used in numeric conversions only:
  81.  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
  82.  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
  83.  */
  84. #define    SIGNOK        0x40    /* +/- is (still) legal */
  85. #define    NDIGITS        0x80    /* no digits detected */
  86.  
  87. #define    DPTOK        0x100    /* (float) decimal point is still legal */
  88. #define    EXPOK        0x200    /* (float) exponent (e+3, etc) still legal */
  89.  
  90. #define    PFXOK        0x100    /* 0x prefix is (still) legal */
  91. #define    NZDIGITS    0x200    /* no zero digits detected */
  92.  
  93. /*
  94.  * Conversion types.
  95.  */
  96. #define    CT_CHAR        0    /* %c conversion */
  97. #define    CT_CCL        1    /* %[...] conversion */
  98. #define    CT_STRING    2    /* %s conversion */
  99. #define    CT_INT        3    /* integer, i.e., strtol or strtoul */
  100. #define    CT_FLOAT    4    /* floating, i.e., strtod */
  101.  
  102. #define u_char unsigned char
  103. #define u_long unsigned long
  104.  
  105. extern u_long strtoul __P((const char*, char**, int));
  106. extern long strtol __P((const char*, char**, int));
  107. static const u_char *__sccl __P((char *tab, const u_char *fmt));
  108. #ifndef _IO_USE_DTOA
  109. extern double atof();
  110. #endif
  111.  
  112. /* If errp != NULL, *errp|=1 if we see a premature EOF;
  113.    *errp|=2 if we an invalid character. */
  114.  
  115. int
  116. _IO_vfscanf(fp, fmt0, ap, errp)
  117.      register _IO_FILE *fp;
  118.      char const *fmt0;
  119.      _IO_va_list ap;
  120.      int *errp;
  121. {
  122.     register const u_char *fmt = (const u_char *)fmt0;
  123.     register int c;        /* character from format, or conversion */
  124.     register _IO_ssize_t width;    /* field width, or 0 */
  125.     register char *p;    /* points into all kinds of strings */
  126.     register int n;        /* handy integer */
  127.     register int flags = 0;    /* flags as defined above */
  128.     register char *p0;    /* saves original value of p when necessary */
  129.     int nassigned;        /* number of fields assigned */
  130.     int nread;        /* number of characters consumed from fp */
  131.     /* Assignments to base and ccfn are just to suppress warnings from gcc.*/
  132.     int base = 0;        /* base argument to strtol/strtoul */
  133.     typedef u_long (*strtoulfn) __P((const char*, char**, int));
  134.     strtoulfn ccfn = 0;
  135.     /* conversion function (strtol/strtoul) */
  136.     char ccltab[256];    /* character class table for %[...] */
  137.     char buf[BUF];        /* buffer for numeric conversions */
  138.     int seen_eof = 0;
  139.  
  140.     /* `basefix' is used to avoid `if' tests in the integer scanner */
  141.     static short basefix[17] =
  142.         { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  143.  
  144.     nassigned = 0;
  145.     nread = 0;
  146.     for (;;) {
  147.         c = *fmt++;
  148.         if (c == 0)
  149.             goto done;
  150.         if (isspace(c)) {
  151.             for (;;) {
  152.                     c = _IO_getc(fp);
  153.                 if (c == EOF) {
  154.                   seen_eof++;
  155.                   break;
  156.                 }
  157.                 if (!isspace(c)) {
  158.                     _IO_ungetc (c, fp);
  159.                     break;
  160.                 }
  161.                 nread++;
  162.             }
  163.             continue;
  164.         }
  165.         if (c != '%')
  166.             goto literal;
  167.         width = 0;
  168.         flags = 0;
  169.         /*
  170.          * switch on the format.  continue if done;
  171.          * break once format type is derived.
  172.          */
  173. again:        c = *fmt++;
  174.         switch (c) {
  175.         case '%':
  176. literal:
  177.                 n = _IO_getc(fp);
  178.             if (n == EOF)
  179.                 goto eof_failure;
  180.             if (n != c) {
  181.                 _IO_ungetc (n, fp);
  182.                 goto match_failure;
  183.             }
  184.             nread++;
  185.             continue;
  186.  
  187.         case '*':
  188.             if (flags) goto control_failure;
  189.             flags = SUPPRESS;
  190.             goto again;
  191.         case 'l':
  192.             if (flags & ~(SUPPRESS | WIDTH)) goto control_failure;
  193.             flags |= LONG;
  194.             goto again;
  195.         case 'L':
  196.             if (flags & ~(SUPPRESS | WIDTH)) goto control_failure;
  197.             flags |= LONGDBL;
  198.             goto again;
  199.         case 'h':
  200.             if (flags & ~(SUPPRESS | WIDTH)) goto control_failure;
  201.             flags |= SHORT;
  202.             goto again;
  203.  
  204.         case '0': case '1': case '2': case '3': case '4':
  205.         case '5': case '6': case '7': case '8': case '9':
  206.             if (flags & ~(SUPPRESS | WIDTH)) goto control_failure;
  207.             flags |= WIDTH;
  208.             width = width * 10 + c - '0';
  209.             goto again;
  210.  
  211.         /*
  212.          * Conversions.
  213.          * Those marked `compat' are for 4.[123]BSD compatibility.
  214.          *
  215.          * (According to ANSI, E and X formats are supposed
  216.          * to the same as e and x.  Sorry about that.)
  217.          */
  218.         case 'D':    /* compat */
  219.             flags |= LONG;
  220.             /* FALLTHROUGH */
  221.         case 'd':
  222.             c = CT_INT;
  223.             ccfn = (strtoulfn)strtol;
  224.             base = 10;
  225.             break;
  226.  
  227.         case 'i':
  228.             c = CT_INT;
  229.             ccfn = (strtoulfn)strtol;
  230.             base = 0;
  231.             break;
  232.  
  233.         case 'O':    /* compat */
  234.             flags |= LONG;
  235.             /* FALLTHROUGH */
  236.         case 'o':
  237.             c = CT_INT;
  238.             ccfn = strtoul;
  239.             base = 8;
  240.             break;
  241.  
  242.         case 'u':
  243.             c = CT_INT;
  244.             ccfn = strtoul;
  245.             base = 10;
  246.             break;
  247.  
  248.         case 'X':
  249.         case 'x':
  250.             flags |= PFXOK;    /* enable 0x prefixing */
  251.             c = CT_INT;
  252.             ccfn = strtoul;
  253.             base = 16;
  254.             break;
  255.  
  256. #ifdef FLOATING_POINT
  257.         case 'E': case 'F':
  258.         case 'e': case 'f': case 'g':
  259.             c = CT_FLOAT;
  260.             break;
  261. #endif
  262.  
  263.         case 's':
  264.             c = CT_STRING;
  265.             break;
  266.  
  267.         case '[':
  268.             fmt = __sccl(ccltab, fmt);
  269.             flags |= NOSKIP;
  270.             c = CT_CCL;
  271.             break;
  272.  
  273.         case 'c':
  274.             flags |= NOSKIP;
  275.             c = CT_CHAR;
  276.             break;
  277.  
  278.         case 'p':    /* pointer format is like hex */
  279.             flags |= POINTER | PFXOK;
  280.             c = CT_INT;
  281.             ccfn = strtoul;
  282.             base = 16;
  283.             break;
  284.  
  285.         case 'n':
  286.             if (flags & SUPPRESS)    /* ??? */
  287.                 continue;
  288.             if (flags & SHORT)
  289.                 *va_arg(ap, short *) = nread;
  290.             else if (flags & LONG)
  291.                 *va_arg(ap, long *) = nread;
  292.             else
  293.                 *va_arg(ap, int *) = nread;
  294.             continue;
  295.  
  296.         /*
  297.          * Disgusting backwards compatibility hacks.    XXX
  298.          */
  299.         case '\0':    /* compat */
  300.                 nassigned = EOF;
  301.             goto done;
  302.  
  303.         default:    /* compat */
  304.             if (isupper(c))
  305.                 flags |= LONG;
  306.             c = CT_INT;
  307.             ccfn = (strtoulfn)strtol;
  308.             base = 10;
  309.             break;
  310.         }
  311.  
  312.         /*
  313.          * We have a conversion that requires input.
  314.          */
  315.         if (_IO_peekc(fp) == EOF)
  316.             goto eof_failure;
  317.  
  318.         /*
  319.          * Consume leading white space, except for formats
  320.          * that suppress this.
  321.          */
  322.         if ((flags & NOSKIP) == 0) {
  323.             n = (unsigned char)*fp->_IO_read_ptr;
  324.             while (isspace(n)) {
  325.             fp->_IO_read_ptr++;
  326.             nread++;
  327.             n = _IO_peekc(fp);
  328.             if (n == EOF)
  329.                 goto eof_failure;
  330.             }
  331.             /* Note that there is at least one character in
  332.                the buffer, so conversions that do not set NOSKIP
  333.                can no longer result in an input failure. */
  334.         }
  335.  
  336.         /*
  337.          * Do the conversion.
  338.          */
  339.         switch (c) {
  340.  
  341.         case CT_CHAR:
  342.             /* scan arbitrary characters (sets NOSKIP) */
  343.             if (width == 0) /* FIXME! */
  344.                 width = 1;
  345.             if (flags & SUPPRESS) {
  346.                 _IO_size_t sum = 0;
  347.                 for (;;) {
  348.                     n = fp->_IO_read_end - fp->_IO_read_ptr;
  349.                 if (n < (int)width) {
  350.                     sum += n;
  351.                     width -= n;
  352.                     fp->_IO_read_ptr += n;
  353.                     if (__underflow(fp) == EOF)
  354.                     if (sum == 0)
  355.                         goto eof_failure;
  356.                     else {
  357.                         seen_eof++;
  358.                         break;
  359.                     }
  360.                 } else {
  361.                     sum += width;
  362.                     fp->_IO_read_ptr += width;
  363.                     break;
  364.                 }
  365.                 }
  366.                 nread += sum;
  367.             } else {
  368.                 _IO_size_t r =
  369.                   (*fp->_jumps->__xsgetn)(fp,
  370.                               (char*)va_arg(ap, char*),
  371.                               width);
  372.                 if (r != width)
  373.                 goto eof_failure;
  374.                 nread += r;
  375.                 nassigned++;
  376.             }
  377.             break;
  378.  
  379.         case CT_CCL:
  380.             /* scan a (nonempty) character class (sets NOSKIP) */
  381.             if (width == 0)
  382.                 width = ~0;    /* `infinity' */
  383.             /* take only those things in the class */
  384.             if (flags & SUPPRESS) {
  385.                 n = 0;
  386.                 while (ccltab[(unsigned char)*fp->_IO_read_ptr]) {
  387.                     n++, fp->_IO_read_ptr++;
  388.                     if (--width == 0)
  389.                     break;
  390.                     if (_IO_peekc(fp) == EOF) {
  391.                     if (n == 0)
  392.                         goto eof_failure;
  393.                     seen_eof++;
  394.                     break;
  395.                     }
  396.                 }
  397.                 if (n == 0)
  398.                     goto match_failure;
  399.             } else {
  400.                 p0 = p = va_arg(ap, char *);
  401.                 while (ccltab[(unsigned char)*fp->_IO_read_ptr]) {
  402.                 *p++ = *fp->_IO_read_ptr++;
  403.                 if (--width == 0)
  404.                     break;
  405.                 if (_IO_peekc(fp) == EOF) {
  406.                     if (p == p0)
  407.                     goto eof_failure;
  408.                     seen_eof++;
  409.                     break;
  410.                 }
  411.                 }
  412.                 n = p - p0;
  413.                 if (n == 0)
  414.                 goto match_failure;
  415.                 *p = 0;
  416.                 nassigned++;
  417.             }
  418.             nread += n;
  419.             break;
  420.  
  421.         case CT_STRING:
  422.             /* like CCL, but zero-length string OK, & no NOSKIP */
  423.             if (width == 0)
  424.                 width = ~0;
  425.             if (flags & SUPPRESS) {
  426.                 n = 0;
  427.                 while (!isspace((unsigned char)*fp->_IO_read_ptr)) {
  428.                     n++, fp->_IO_read_ptr++;
  429.                     if (--width == 0)
  430.                         break;
  431.                     if (_IO_peekc(fp) == EOF) {
  432.                         seen_eof++;
  433.                         break;
  434.                     }
  435.                 }
  436.                 nread += n;
  437.             } else {
  438.                 p0 = p = va_arg(ap, char *);
  439.                 while (!isspace((unsigned char)*fp->_IO_read_ptr)) {
  440.                     *p++ = *fp->_IO_read_ptr++;
  441.                     if (--width == 0)
  442.                         break;
  443.                     if (_IO_peekc(fp) == EOF) {
  444.                         seen_eof++;
  445.                         break;
  446.                     }
  447.                 }
  448.                 *p = 0;
  449.                 nread += p - p0;
  450.                 nassigned++;
  451.             }
  452.             continue;
  453.  
  454.         case CT_INT:
  455.             /* scan an integer as if by strtol/strtoul */
  456.             if (width == 0 || width > sizeof(buf) - 1)
  457.                 width = sizeof(buf) - 1;
  458.             flags |= SIGNOK | NDIGITS | NZDIGITS;
  459.             for (p = buf; width; width--) {
  460.                 c = (unsigned char)*fp->_IO_read_ptr;
  461.                 /*
  462.                  * Switch on the character; `goto ok'
  463.                  * if we accept it as a part of number.
  464.                  */
  465.                 switch (c) {
  466.  
  467.                 /*
  468.                  * The digit 0 is always legal, but is
  469.                  * special.  For %i conversions, if no
  470.                  * digits (zero or nonzero) have been
  471.                  * scanned (only signs), we will have
  472.                  * base==0.  In that case, we should set
  473.                  * it to 8 and enable 0x prefixing.
  474.                  * Also, if we have not scanned zero digits
  475.                  * before this, do not turn off prefixing
  476.                  * (someone else will turn it off if we
  477.                  * have scanned any nonzero digits).
  478.                  */
  479.                 case '0':
  480.                     if (base == 0) {
  481.                         base = 8;
  482.                         flags |= PFXOK;
  483.                     }
  484.                     if (flags & NZDIGITS)
  485.                         flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  486.                     else
  487.                         flags &= ~(SIGNOK|PFXOK|NDIGITS);
  488.                     goto ok;
  489.  
  490.                 /* 1 through 7 always legal */
  491.                 case '1': case '2': case '3':
  492.                 case '4': case '5': case '6': case '7':
  493.                     base = basefix[base];
  494.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  495.                     goto ok;
  496.  
  497.                 /* digits 8 and 9 ok iff decimal or hex */
  498.                 case '8': case '9':
  499.                     base = basefix[base];
  500.                     if (base <= 8)
  501.                         break;    /* not legal here */
  502.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  503.                     goto ok;
  504.  
  505.                 /* letters ok iff hex */
  506.                 case 'A': case 'B': case 'C':
  507.                 case 'D': case 'E': case 'F':
  508.                 case 'a': case 'b': case 'c':
  509.                 case 'd': case 'e': case 'f':
  510.                     /* no need to fix base here */
  511.                     if (base <= 10)
  512.                         break;    /* not legal here */
  513.                     flags &= ~(SIGNOK | PFXOK | NDIGITS);
  514.                     goto ok;
  515.  
  516.                 /* sign ok only as first character */
  517.                 case '+': case '-':
  518.                     if (flags & SIGNOK) {
  519.                         flags &= ~SIGNOK;
  520.                         goto ok;
  521.                     }
  522.                     break;
  523.  
  524.                 /* x ok iff flag still set & 2nd char */
  525.                 case 'x': case 'X':
  526.                     if (flags & PFXOK && p == buf + 1) {
  527.                         base = 16;    /* if %i */
  528.                         flags &= ~PFXOK;
  529.                         goto ok;
  530.                     }
  531.                     break;
  532.                 }
  533.  
  534.                 /*
  535.                  * If we got here, c is not a legal character
  536.                  * for a number.  Stop accumulating digits.
  537.                  */
  538.                 break;
  539.         ok:
  540.                 /*
  541.                  * c is legal: store it and look at the next.
  542.                  */
  543.                 *p++ = c;
  544.                 fp->_IO_read_ptr++;
  545.                 if (_IO_peekc(fp) == EOF) {
  546.                     seen_eof++;
  547.                     break;        /* EOF */
  548.                 }
  549.                 }
  550.             /*
  551.              * If we had only a sign, it is no good; push
  552.              * back the sign.  If the number ends in `x',
  553.              * it was [sign] '0' 'x', so push back the x
  554.              * and treat it as [sign] '0'.
  555.              */
  556.             if (flags & NDIGITS) {
  557.                 if (p > buf)
  558.                     (void) _IO_ungetc(*(u_char *)--p, fp);
  559.                 goto match_failure;
  560.             }
  561.             c = ((u_char *)p)[-1];
  562.             if (c == 'x' || c == 'X') {
  563.                 --p;
  564.                 (void) _IO_ungetc (c, fp);
  565.             }
  566.             if ((flags & SUPPRESS) == 0) {
  567.                 u_long res;
  568.  
  569.                 *p = 0;
  570.                 res = (*ccfn)(buf, (char **)NULL, base);
  571.                 if (flags & POINTER)
  572.                     *va_arg(ap, void **) = (void *)res;
  573.                 else if (flags & SHORT)
  574.                     *va_arg(ap, short *) = res;
  575.                 else if (flags & LONG)
  576.                     *va_arg(ap, long *) = res;
  577.                 else
  578.                     *va_arg(ap, int *) = res;
  579.                 nassigned++;
  580.             }
  581.             nread += p - buf;
  582.             break;
  583.  
  584. #ifdef FLOATING_POINT
  585.         case CT_FLOAT:
  586.             /* scan a floating point number as if by strtod */
  587.             if (width == 0 || width > sizeof(buf) - 1)
  588.                 width = sizeof(buf) - 1;
  589.             flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
  590.             for (p = buf; width; width--) {
  591.                 c = (unsigned char)*fp->_IO_read_ptr;
  592.                 /*
  593.                  * This code mimicks the integer conversion
  594.                  * code, but is much simpler.
  595.                  */
  596.                 switch (c) {
  597.  
  598.                 case '0': case '1': case '2': case '3':
  599.                 case '4': case '5': case '6': case '7':
  600.                 case '8': case '9':
  601.                     flags &= ~(SIGNOK | NDIGITS);
  602.                     goto fok;
  603.  
  604.                 case '+': case '-':
  605.                     if (flags & SIGNOK) {
  606.                         flags &= ~SIGNOK;
  607.                         goto fok;
  608.                     }
  609.                     break;
  610.                 case '.':
  611.                     if (flags & DPTOK) {
  612.                         flags &= ~(SIGNOK | DPTOK);
  613.                         goto fok;
  614.                     }
  615.                     break;
  616.                 case 'e': case 'E':
  617.                     /* no exponent without some digits */
  618.                     if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
  619.                         flags =
  620.                             (flags & ~(EXPOK|DPTOK)) |
  621.                             SIGNOK | NDIGITS;
  622.                         goto fok;
  623.                     }
  624.                     break;
  625.                 }
  626.                 break;
  627.         fok:
  628.                 *p++ = c;
  629.                 fp->_IO_read_ptr++;
  630.                 if (_IO_peekc(fp) == EOF) {
  631.                     seen_eof++;
  632.                     break;    /* EOF */
  633.                 }
  634.             }
  635.             /*
  636.              * If no digits, might be missing exponent digits
  637.              * (just give back the exponent) or might be missing
  638.              * regular digits, but had sign and/or decimal point.
  639.              */
  640.             if (flags & NDIGITS) {
  641.                 if (flags & EXPOK) {
  642.                     /* no digits at all */
  643.                     while (p > buf)
  644.                         _IO_ungetc (*(u_char *)--p, fp);
  645.                     goto match_failure;
  646.                 }
  647.                 /* just a bad exponent (e and maybe sign) */
  648.                 c = *(u_char *)--p;
  649.                 if (c != 'e' && c != 'E') {
  650.                     (void) _IO_ungetc (c, fp);/* sign */
  651.                     c = *(u_char *)--p;
  652.                 }
  653.                 (void) _IO_ungetc (c, fp);
  654.             }
  655.             if ((flags & SUPPRESS) == 0) {
  656.                 double res;
  657.                 *p = 0;
  658. #ifdef _IO_USE_DTOA
  659.                 res = _IO_strtod(buf, NULL);
  660. #else
  661.                 res = atof(buf);
  662. #endif
  663.                 if (flags & LONG)
  664.                     *va_arg(ap, double *) = res;
  665.                 else
  666.                     *va_arg(ap, float *) = res;
  667.                 nassigned++;
  668.             }
  669.             nread += p - buf;
  670.             break;
  671. #endif /* FLOATING_POINT */
  672.         }
  673.     }
  674. eof_failure:
  675.     seen_eof++;
  676. input_failure:
  677.     if (nassigned == 0)
  678.         nassigned = -1;
  679. control_failure:
  680. match_failure:
  681.     if (errp)
  682.         *errp |= 2;
  683. done:
  684.     if (errp && seen_eof)
  685.         *errp |= 1;
  686.     return (nassigned);
  687. }
  688.  
  689. /*
  690.  * Fill in the given table from the scanset at the given format
  691.  * (just after `[').  Return a pointer to the character past the
  692.  * closing `]'.  The table has a 1 wherever characters should be
  693.  * considered part of the scanset.
  694.  */
  695. static const u_char *__sccl(tab, fmt)
  696.      register char *tab;
  697.      register const u_char *fmt;
  698. {
  699.     register int c, n, v;
  700.  
  701.     /* first `clear' the whole table */
  702.     c = *fmt++;        /* first char hat => negated scanset */
  703.     if (c == '^') {
  704.         v = 1;        /* default => accept */
  705.         c = *fmt++;    /* get new first char */
  706.     } else
  707.         v = 0;        /* default => reject */
  708.     /* should probably use memset here */
  709.     for (n = 0; n < 256; n++)
  710.         tab[n] = v;
  711.     if (c == 0)
  712.         return (fmt - 1);/* format ended before closing ] */
  713.  
  714.     /*
  715.      * Now set the entries corresponding to the actual scanset
  716.      * to the opposite of the above.
  717.      *
  718.      * The first character may be ']' (or '-') without being special;
  719.      * the last character may be '-'.
  720.      */
  721.     v = 1 - v;
  722.     for (;;) {
  723.         tab[c] = v;        /* take character c */
  724. doswitch:
  725.         n = *fmt++;        /* and examine the next */
  726.         switch (n) {
  727.  
  728.         case 0:            /* format ended too soon */
  729.             return (fmt - 1);
  730.  
  731.         case '-':
  732.             /*
  733.              * A scanset of the form
  734.              *    [01+-]
  735.              * is defined as `the digit 0, the digit 1,
  736.              * the character +, the character -', but
  737.              * the effect of a scanset such as
  738.              *    [a-zA-Z0-9]
  739.              * is implementation defined.  The V7 Unix
  740.              * scanf treats `a-z' as `the letters a through
  741.              * z', but treats `a-a' as `the letter a, the
  742.              * character -, and the letter a'.
  743.              *
  744.              * For compatibility, the `-' is not considerd
  745.              * to define a range if the character following
  746.              * it is either a close bracket (required by ANSI)
  747.              * or is not numerically greater than the character
  748.              * we just stored in the table (c).
  749.              */
  750.             n = *fmt;
  751.             if (n == ']' || n < c) {
  752.                 c = '-';
  753.                 break;    /* resume the for(;;) */
  754.             }
  755.             fmt++;
  756.             do {        /* fill in the range */
  757.                 tab[++c] = v;
  758.             } while (c < n);
  759. #if 1    /* XXX another disgusting compatibility hack */
  760.             /*
  761.              * Alas, the V7 Unix scanf also treats formats
  762.              * such as [a-c-e] as `the letters a through e'.
  763.              * This too is permitted by the standard....
  764.              */
  765.             goto doswitch;
  766. #else
  767.             c = *fmt++;
  768.             if (c == 0)
  769.                 return (fmt - 1);
  770.             if (c == ']')
  771.                 return (fmt);
  772. #endif
  773.             break;
  774.  
  775.         case ']':        /* end of scanset */
  776.             return (fmt);
  777.  
  778.         default:        /* just another character */
  779.             c = n;
  780.             break;
  781.         }
  782.     }
  783.     /* NOTREACHED */
  784. }
  785.